<?php
/**
 * Description of Substring
 *
 * @author NguyenVanTien
 */
class Zend_View_Helper_Substring extends Zend_View_Helper_Abstract {
    
    /**
     * Substring view helper
     * @param string $string
     * @param int $length
     * @param int $start
     * @return string $res
     */
    public function substring($string, $length, $start = 0){
        $res = '';
        
        $arrWithSpace = explode(' ', $string);
        
        //implore array still string length < $lenght
        foreach($arrWithSpace as $item){
            if(strlen($res) < (int)$length){
                $res .= $item.' ';
            }else{
                //add '...' when end string if have substring
                $res .= '...';
                break;
            }
        }
        
        //substring if $start > 0
        $res = substr($res, $start);
        
        return $res;
    }
}